package com.tim.app.ui.activity;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.application.library.dialog.LoadingDialog;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.ui.view.LSWebView;
import com.tim.app.util.ViewGT;


public class GeneralWebActivity extends BaseActivity implements LSWebView.onReceiveInfoListener, LSWebView.OnOpenUrlListener {

    private static final String TAG = "GeneralWebView";
    private View title_bar;
    private ImageButton ib_back;
    private TextView mTitleView;

    private LSWebView mWebView;

    private LoadingDialog loadingDialog;
    private String web_url = "";

    private boolean isFromPush = false;//是否来自推送

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general_web;
    }

    @Override
    public void initView() {
        title_bar = findViewById(R.id.title_bar);
        mTitleView = (TextView) findViewById(R.id.title);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        mWebView = (LSWebView) findViewById(R.id.webview);
        mWebView.setInterceptBack(true);
        mWebView.setOnReceiveInfoListener(this);
        mWebView.setOnOpenUrlListener(this);
        ib_back.setOnClickListener(this);

        loadingDialog = new LoadingDialog(this);
        loadingDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    public void initData() {
        web_url = getIntent().getStringExtra(AppKey.WEB_URL);
        mWebView.openUrl(web_url);
        isFromPush = getIntent().getExtras().getBoolean(AppKey.IS_FROM_PUSH);
    }

    @Override
    public void onOpenUrl(String url) {

    }

    @Override
    public void showLoadingLayout() {
        if (null != loadingDialog) {
            loadingDialog.show();
        }
    }

    @Override
    public void hideLoadingLayout() {
        if (null != loadingDialog) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onReceivedError() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void OnReceiveTitle(String title) {
        mTitleView.setText(title);
        title_bar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReceiveIcon(Bitmap icon) {

    }

    @Override
    public void setShareInfo(String title, String desc, String image) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != loadingDialog) {
            loadingDialog.cancel();
            loadingDialog = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (isFromPush) {
            ViewGT.gotoMainActivity(GeneralWebActivity.this);
        } else {
            if (mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                super.onBackPressed();
            }
        }

    }

}
